<template>
  <el-form :model="publish" ref="rules" label-width="100px" class="demo-ruleForm" >
    <el-form-item label="发行批次" prop="activityStartTime">
      <el-select v-model="publish.couponPublishId" placeholder="请选择发行批次">
        <el-option v-for="item in publishs" :key="item.couponPublishId" :label="item.couponPublishName" :value="item.couponPublishId">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="领取总量" prop="sendNum">
      <el-input-number v-model="publish.sendNum" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="单人总量" prop="limit">
      <el-input-number v-model="publish.limit" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item label="限制规则" prop="sendLimitType">
      <el-select v-model="publish.sendLimitType" :disabled="disableType" placeholder="选择限制规则">
        <el-option v-for="item in sendLimits" :key="item.value" :label="item.label" :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="数量限制" prop="limitNum">
      <el-input-number v-model="publish.limitNum" :min="1"></el-input-number>
    </el-form-item>
    <el-form-item style="text-align:right;">
      <el-button type="primary" @click="submitForm('rules')" size="mini">保存</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  props: {
    type: {
      type: Number,
      default: null,
    },
  },
  data() {
    return {
      disableType:false,
      publishs: [],
      publish: {

      },
      sendLimits: [{ value: 1, label: "活动赠送券" }, { value: 2, label: "单日限制数量" }, { value: 3, label: "单周限制数量" }, { value: 4, label: "单月限制数量" }],
    }
  },
  created(){
      if(this.type){
          this.publish.sendLimitType = this.type;
          this.disableType=true;
      }
  },
  mounted() {
      
    this.loadCouponPublish();
  },
  methods: {
    loadCouponPublish() {
      this.axios
        .get("/fc/publish/list")
        .then(response => {
          this.publishs = response.data.data;
        })
        .catch(response => {
          console.log("response -   " + response);
        });
    },
    submitForm(formName) { //保存活动商品
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.saveActivityCoupon();
        } else {
          return false;
        }
      });
    },
    saveActivityCoupon() {
      this.publishs.forEach(publish => {
        if (publish.couponPublishId == this.publish.couponPublishId) {
          this.publish.publishName = publish.couponPublishName;
        }
      });
      // console.log('this.publish - ',this.publish);
      this.$emit("close",this.publish);
      this.publish = {};
    },
  }
}
</script>
